package com.example.myapp;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;

import java.text.DecimalFormat;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

public class MyActivity extends Activity implements OnClickListener {

    private Button clearButton;
    private Button deleteButton;
    private Button dotButton;
    private Button addButton;
    private Button reduceButton;
    private Button multiplyButton;
    private Button dividedButton;
    private Button zeroButton;
    private Button oneButton;
    private Button twoButton;
    private Button threeButton;
    private Button fourButton;
    private Button fiveButton;
    private Button sixButton;
    private Button sevenButton;
    private Button eightButton;
    private Button nineButton;
    private Button equalButton;
    private Button leftBracketsButton;
    private Button rightBracketsButton;
    private TextView textView;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
        setOnClickListener();
    }

    @Override
    public void onClick(View v) {
        String currentText = textView.getText().toString();
        switch (v.getId()) {
            case R.id.zeroButton:
                textView.setText(currentText+"0");
                break;
            case R.id.oneButton:
                textView.setText(currentText+"1");
                break;
            case R.id.twoButton:
                textView.setText(currentText+"2");
                break;
            case R.id.threeButton:
                textView.setText(currentText+"3");
                break;
            case R.id.fourButton:
                textView.setText(currentText+"4");
                break;
            case R.id.fiveButton:
                textView.setText(currentText+"5");
                break;
            case R.id.sixButton:
                textView.setText(currentText+"6");
                break;
            case R.id.sevenButton:
                textView.setText(currentText+"7");
                break;
            case R.id.eightButton:
                textView.setText(currentText+"8");
                break;
            case R.id.nineButton:
                textView.setText(currentText+"9");
                break;
            case R.id.dotButton:
                textView.setText(currentText+".");
                break;
            case R.id.clearButton:
                textView.setText("");
                break;
            case R.id.multiplyButton:
                textView.setText(currentText+"×");
                break;
            case R.id.dividedButton:
                textView.setText(currentText+"÷");
                break;
            case R.id.addButton:
                textView.setText(currentText+"+");
                break;
            case R.id.reduceButton:
                textView.setText(currentText+"-");
                break;
            case R.id.leftBracketsButton:
                textView.setText(currentText+"(");
                break;
            case R.id.rightBracketsButton:
                textView.setText(currentText+")");
                break;
            case R.id.deleteButton:
                if (currentText.length() >0) {
                    textView.setText(currentText.substring(0, currentText.length()-1));
                }
                break;
            case R.id.equalButton:
                if (TextUtils.isEmpty(currentText)) {
                    Toast.makeText(getApplicationContext(), "请输入表达式！", Toast.LENGTH_LONG).show();
                    return;
                }
                try {
                    Double result = (Double) AviatorEvaluator.execute("1.0*"+currentText.replace("×", "*").replace("÷", "/"));
                    textView.setText(result.toString());
                    //todo 格式化结果
                    Log.i("result", result.toString());
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "表达式不合法！", Toast.LENGTH_LONG).show();
                    Log.e("error", e.getMessage());
                    Log.e("error", "表达式有误！");
                }
                break;
        }
    }

    /**
     * 初始化
     */
    private void init() {
        textView            = (TextView) findViewById(R.id.textView);
        deleteButton        = (Button) findViewById(R.id.deleteButton);
        dotButton           = (Button) findViewById(R.id.dotButton);
        addButton           = (Button) findViewById(R.id.addButton);
        reduceButton        = (Button) findViewById(R.id.reduceButton);
        multiplyButton      = (Button) findViewById(R.id.multiplyButton);
        dividedButton       = (Button) findViewById(R.id.dividedButton);
        zeroButton          = (Button) findViewById(R.id.zeroButton);
        oneButton           = (Button) findViewById(R.id.oneButton);
        twoButton           = (Button) findViewById(R.id.twoButton);
        threeButton         = (Button) findViewById(R.id.threeButton);
        fourButton          = (Button) findViewById(R.id.fourButton);
        fiveButton          = (Button) findViewById(R.id.fiveButton);
        sixButton           = (Button) findViewById(R.id.sixButton);
        sevenButton         = (Button) findViewById(R.id.sevenButton);
        eightButton         = (Button) findViewById(R.id.eightButton);
        nineButton          = (Button) findViewById(R.id.nineButton);
        equalButton         = (Button) findViewById(R.id.equalButton);
        leftBracketsButton  = (Button) findViewById(R.id.leftBracketsButton);
        rightBracketsButton = (Button) findViewById(R.id.rightBracketsButton);
        clearButton         = (Button) findViewById(R.id.clearButton);
        //字典初始化
    }

    /**
     * 事件绑定
     */
    private void setOnClickListener() {
        clearButton.setOnClickListener(this);
        oneButton.setOnClickListener(this);
        twoButton.setOnClickListener(this);
        threeButton.setOnClickListener(this);
        fourButton.setOnClickListener(this);
        fiveButton.setOnClickListener(this);
        sixButton.setOnClickListener(this);
        sevenButton.setOnClickListener(this);
        eightButton.setOnClickListener(this);
        nineButton.setOnClickListener(this);
        zeroButton.setOnClickListener(this);
        addButton.setOnClickListener(this);
        reduceButton.setOnClickListener(this);
        multiplyButton.setOnClickListener(this);
        dividedButton.setOnClickListener(this);
        deleteButton.setOnClickListener(this);
        dotButton.setOnClickListener(this);
        equalButton.setOnClickListener(this);
        leftBracketsButton.setOnClickListener(this);
        rightBracketsButton.setOnClickListener(this);
    }

}
